package com.bobo.web.provider.util.commutils;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by LiLong on 14-8-26.
 */
public class ExcelWriter {

    private XSSFWorkbook workbook;//工作本
    private XSSFSheet currSheet;// 当前表Sheet
    private XSSFRow currRow;//当前行Row
    private XSSFCell currCell;//当前单元格Cell
    // private HSSFDataFormat dataFormat;
    private XSSFCellStyle dateStyle;
    private String sheetName;
    private static int MAX_EXCEL_COLUMNS = 256;//设置一个sheet最大列数
    private static int MAX_EXCEL_ROW = 50000;//设置一个sheet的最大行数
    private OutputStream os = null;// 文件二进制输出流
    //private int currPosition; // 当前位置
    private int numOfSheets;// Sheet数量
    private short currNumOfColumns;//记录当前的列数
    private int currNumOfRow;//记录当前的行数

    public ExcelWriter(OutputStream outputStream) {
        if (outputStream != null) {
            os = outputStream;
            workbook = new XSSFWorkbook();
            currSheet = null;
            currRow = null;
            currCell = null;
            numOfSheets = 0;
            currNumOfRow = 0;
            currNumOfColumns = 0;
        }
    }

    /**
     * 创建一张新的Sheet工作表
     */
    public void createNewSheet(String sheetName){
        this.sheetName = sheetName;
        currSheet = workbook.createSheet(sheetName);
        numOfSheets++;//记录sheet的数量
    }

    /**
     * 设置sheet工作区的标题－－－－－设置标题属性部分未完成
     * @param title
     * @param height
     * @param color
     * @return
     */
    public boolean setTitle(String title,int cellStart,int cellEnd,short height,short color){
        if(currNumOfRow+1>MAX_EXCEL_ROW){//如果当前的行数超过规定的行数
            return false;
        }

        XSSFRow row = currSheet.createRow(currNumOfRow++);
        currSheet.addMergedRegion(new CellRangeAddress(currNumOfRow-1,currNumOfRow-1,cellStart,cellEnd));
        XSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment((short)XSSFCellStyle.ALIGN_CENTER);//表头样式
        XSSFFont font = workbook.createFont();//设置字体属性
        font.setFontHeightInPoints(height);
        font.setColor(color);
        style.setFont(font);

        //创建单元格对象,写入标题
        XSSFCell titleCell = row.createCell((short)0);
        titleCell.setCellStyle(style);
        titleCell.setCellValue(new XSSFRichTextString(title));


        return true;
    }

    /**
     * 设置sheet的表头
     * @param field
     * @param height
     * @param color
     * @return
     */
    public boolean setField(String []field,short height,short color){
        if(field.length > MAX_EXCEL_COLUMNS)//如果超出了规定的列数
            return false;
        XSSFRow row = currSheet.createRow(currNumOfRow++);
        XSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment((short)XSSFCellStyle.ALIGN_CENTER);//表头样式
        XSSFFont font = workbook.createFont();//设置字体属性
        font.setFontHeightInPoints(height);
        font.setColor(color);
        style.setFont(font);
        //style.setWrapText(true);
        for(short i=0; i<field.length; i++){
            XSSFCell cell = row.createCell(i);
            cell.setCellStyle(style);
            cell.setCellValue(new XSSFRichTextString(field[i]));
        }
        return true;
    }

    /**
     * 创建新的一行
     * @return
     */
    public boolean createNewRow(){
        if(currNumOfRow+1>MAX_EXCEL_ROW)//如果超出规定的行数最大值
            this.createNewSheet(sheetName+numOfSheets);
        currRow = currSheet.createRow(currNumOfRow++);
        currNumOfColumns = 0;
        return true;
    }

    public void setRowValue(String []dates){
        for(String date:dates){
            this.createNewCell(date);
        }
    }
    /**
     * 创建新的一行，并对其赋值
     * @param
     * @return
     */
    public boolean creatNewRow(String... dates){
        if(!this.createNewRow())return false;
        this.setRowValue(dates);
        return true;
    }


    /**
     * 创建新的一个的单元格
     * @return
     */
    public boolean createNewCell(){
        if(currNumOfColumns+1>MAX_EXCEL_COLUMNS)//如果超出规定的列数最大值
            return false;
        currCell = currRow.createCell(currNumOfColumns++);
        return true;
    }

    /**
     * 对当前的单元格cell赋值
     * @param date
     */
    public void setCellValue(String date){
        currCell.setCellValue(new XSSFRichTextString(date));
    }

    /**
     * 创建一个新的单元格，并对其赋值
     * @param date
     * @return
     */
    public boolean createNewCell(Object date){
        if(!this.createNewCell())return false;
        if(dateStyle!=null)
            currCell.setCellStyle(dateStyle);
        if (date != null) {
            this.setCellValue(date.toString());
        } else {
            this.setCellValue("");
        }
        return true;
    }

    /**
     * 设置日期的显示格式
     * @param format
     */
    public void setCellDataFormat(String format){
        if(dateStyle == null)
            dateStyle = workbook.createCellStyle();
        XSSFDataFormat dataFormat = workbook.createDataFormat();
        if("".equals(format) || format==null)
            format = "yyyy-mm-dd";
        dateStyle.setDataFormat(dataFormat.getFormat(format));
    }

    public void close(){
        try{
            workbook.write(os);
            if(os != null){//关闭输出流POI不负责关闭
                os.close();
            }
        }catch(IOException e){
            os = null;
            e.printStackTrace();
        }
    }
}
